/*--------------------------------------------------------------------------------*/
#ifndef __TEXT__
#define __TEXT__
/*--------------------------------------------------------------------------------*/
#include "primitives.h"
#include "object.h"
#include "defs.h"
#include "array.h"
#include "ref_to_.h"
#include <string>
#include <cstdlib>
#include <cstdio>
/*--------------------------------------------------------------------------------*/
class TEXT
{

public:

    // .. CONSTRUCTORS

    TEXT() : Text()
    {

    }

    TEXT( const char *text ) : Text( text )
    {

    }

    TEXT( const std::string & str ) : Text( str )
    {

    }

    TEXT( const TEXT & text ) : Text( text.GetArray() )
    {

    }

    TEXT( const CHAR & character )
    {
    	Text = "";
    	Text += character;
    }

    ~TEXT()
    {

    }

    // .. METHODS

    VOID Clear()
    {
    	Text.clear();
    }

    VOID Add( const CHAR ch )
    {
    	Text.push_back( ch );
    }

    const CHAR *GetArray() const
    {
    	return Text.c_str();
    }

    COUNT GetLength() const
    {
    	return Text.length();
    }

    INDEX FindLast( const CHAR character ) const
    {
    	return ( INDEX )Text.find_last_of( character );
    }

    const TEXT SubString( const INDEX start, const COUNT length = std::string::npos ) const
    {
    	return TEXT( Text.substr( start, length ) );
    }

    VOID Trim()
    {
    	INT
			ret;

    	while( ( ret = Text.find( "\n" ) ) != std::string::npos )
    	{
    		Text.erase( ret );
    	}
    }

    VOID RemoveSpaces()
    {
    	INT
			ret;

    	while( ( ret = Text.find( " " ) ) != std::string::npos )
    	{
    		Text.erase( ret );
    	}
    }

    VOID RemoveLastChars( const COUNT count )
    {
    	Text.erase( GetLength() - count );
    }

    // .. OPERATORS

    CHAR & operator[]( const INDEX item_index )
    {
        return Text[ item_index ];
    }

    const CHAR & operator[]( const INDEX item_index ) const
    {
        return Text[ item_index ];
    }

    TEXT & operator=( const TEXT & other )
    {
    	Text = other.Text;
    	return *this;
    }

    TEXT operator+( const TEXT & other )
    {
    	TEXT result( *this );
    	result += other;
    	return result;
    }

    TEXT & operator+=( const TEXT & other )
    {
    	Text += other.Text;
    	return *this;
    }

    BOOL operator==( const TEXT & other ) const
	{
    	return Text == other.Text;
	}

    BOOL operator!=( const TEXT & other ) const
	{
    	return Text != other.Text;
	}

    BOOL operator<( const TEXT & other ) const
	{
    	return Text < other.Text;
	}

    // .. FUNCTIONS

    static TEXT GetTextFromReal( const REAL & real, const COUNT precision = 2 )
	{
		TEXT
			result;

		CHAR
			str[128];

		sprintf( str, "%.*f", precision, real );

		result = str;

		return result;
	}

    static TEXT GetTextFromInteger( const INT & integer )
	{
		TEXT
			result;

		CHAR
			str[128];

		sprintf( str, "%i", integer );

		result = str;

		return result;
	}

    // -- PRIVATE

private :

    // .. ATTRIBUTES
    
    std::string Text;
};
/*--------------------------------------------------------------------------------*/
TEXT operator+( const TEXT & left, const TEXT & right );
/*--------------------------------------------------------------------------------*/
#endif
